using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.Open.CommonAPIs;
using System;
using System.Linq;
namespace Senparc.Weixin.Open.OAuth
{
	public static class OAuthApi
	{
		public static string GetAuthorizeUrl(string appId, string componentAppId, string redirectUrl, string state, OAuthScope[] scopes, string responseType = "code")
		{
			string arg_5B_0 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type={2}&scope={3}&state={4}&component_appid={5}#wechat_redirect";
			object[] array = new object[6];
			array[0] = appId;
			array[1] = redirectUrl.UrlEncode();
			array[2] = responseType;
			array[3] = string.Join(",", 
				from z in scopes
				select z.ToString());
			array[4] = state;
			array[5] = componentAppId;
			return string.Format(arg_5B_0, array);
		}
		public static OAuthAccessTokenResult GetAccessToken(string appId, string componentAppid, string componentAccessToken, string code, string grantType = "authorization_code")
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={0}&code={1}&grant_type={2}&component_appid={3}&component_access_token={4}", new object[]
			{
				appId,
				code,
				grantType,
				componentAppid,
				componentAccessToken
			});
			return CommonJsonSend.Send<OAuthAccessTokenResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000);
		}
		public static OAuthAccessTokenResult RefreshToken(string appId, string refreshToken, string componentAppid, string componentAccessToken, string grantType = "refresh_token")
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/sns/oauth2/component/refresh_token?appid={0}&grant_type={1}&component_appid={2}&component_access_token={3}&refresh_token={4}", new object[]
			{
				appId,
				grantType,
				componentAppid,
				componentAccessToken,
				refreshToken
			});
			return CommonJsonSend.Send<OAuthAccessTokenResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000);
		}
		public static OAuthUserInfo GetUserInfo(string accessToken, string openId, Language lang = Language.zh_CN)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang={2}", accessToken, openId, lang);
			return CommonJsonSend.Send<OAuthUserInfo>(null, urlFormat, null, CommonJsonSendType.GET, 10000);
		}
	}
}
